// src/utils/authRequest.js
import axios from 'axios'
import { useCounterStore } from '@/stores/counter'

const authHttp = axios.create({
  baseURL:'http://localhost:9520'
})

// 请求拦截器（自动添加Token）
authHttp.interceptors.request.use(config => {
  const userStore = useCounterStore()
  if (userStore.token) {
    config.headers['X-Token']= userStore.token
  }
  return config
})

// 响应拦截器（处理401错误）
authHttp.interceptors.response.use(
  response => response.data,
  error => {
    if (error.response?.status === 401) {
      useUserStore().logout()
      window.location.href = '/login'
    }
    return Promise.reject(error)
  }
)

export default authHttp